#!/usr/bin/python
# coding=utf-8

import sys, datetime, json, logging

from xpy3lib.utils import db_utils as util

from model.config import app_config
from xpy3lib.XLogger import XLogger
from xpy3lib.utils.XUtils import XUtils

from model.MODELJob import MODELJob

profile = 'dev'


def model():
    status = 0
    try:
        info = XUtils.get_git_info()
    except Exception as e:
        info = {'message': 'no_git'}

    level = logging.ERROR if profile == 'prod' else logging.NOTSET
    XLogger.getInstance(p_level=level, p_name_key_word='model').logger.error(
        '\n%s' % json.dumps(info, sort_keys=True, indent=4))

    config = app_config[profile]



    try:
        db_conn_mpp = util.getConnectionDb2(config.DB_HOST_MPP_DB2,
                                            config.DB_PORT_MPP_DB2,
                                            config.DB_DBNAME_MPP_DB2,
                                            config.DB_USER_MPP_DB2,
                                            config.DB_PASSWORD_MPP_DB2)
        XLogger.getInstance().logger.info('connect db_conn_mpp success')

        # db_conn_dbprod7 = util.getConnectionDb2(config.DB_HOST_DBPROD7_DB2,
        #                                         config.DB_PORT_DBPROD7_DB2,
        #                                         config.DB_DBNAME_DBPROD7_DB2,
        #                                         config.DB_USER_DBPROD7_DB2,
        #                                         config.DB_PASSWORD_DBPROD7_DB2)
        # XLogger.getInstance().logger.info('connect db_conn_dbprod7 success')
        # db_conn_rds = util.getConnectionDb2(config.DB_HOST_RDS_DB2,
        #                                     config.DB_PORT_RDS_DB2,
        #                                     config.DB_DBNAME_RDS_DB2,
        #                                     config.DB_USER_RDS_DB2,
        #                                     config.DB_PASSWORD_RDS_DB2)
        # XLogger.getInstance().logger.info('connect db_conn_rds success')
    except Exception as e:
        db_conn_mpp = None
        db_conn_rds = None
        db_conn_dbprod7 = None
        XLogger.getInstance().logger.critical(str(e))

    try:
        run_model(p_config=config,
                       p_db_conn_mpp=db_conn_mpp
                       )
    except Exception as e:
        XLogger.getInstance().logger.critical(str(e))
    # run_model(p_config=config, p_db_conn_mpp=db_conn_mpp, p_db_conn_rds=db_conn_rds, p_db_conn_dbprod7=db_conn_dbprod7,
    #             p_account=account, p_month=month)

    try:
        util.closeConnection(db_conn_mpp)
        # util.closeConnection(db_conn_rds)
        # util.closeConnection(db_conn_dbprod7)
    except Exception as e:
        XLogger.getInstance().logger.error(str(e))
        status = 1

    return status


def run_model(p_config=None, p_db_conn_mpp=None):
    p_ver = 'TM2021102400'
    p_type = 1
    if p_db_conn_mpp is not None:
        sheet7_B4, sheet7_C4, sheet7_D4, sheet7_B5, sheet7_C5, sheet7_B6, sheet7_C6, sheet7_B7, sheet7_C7, sheet7_D7, sheet7_B8, sheet7_C8, sheet7_D8, sheet7_B9, sheet7_C9, sheet7_B13, sheet7_C13, sheet7_D13, sheet7_B14, sheet7_C14, sheet7_D14, type2_best_B6, type2_best_C6, type3_best_B4_ADD, type3_best_C4_ADD, type4_best_B4_ADD, type4_best_C4_ADD, type4_best_total_ADD \
            = MODELJob(p_config=p_config,
                       p_db_conn_mpp=p_db_conn_mpp, p_ver=p_ver, p_type=p_type).execute()
        data = {'B4': sheet7_B4, 'C4': sheet7_C4, 'D4': sheet7_D4, 'B5': sheet7_B5, 'C5': sheet7_C5, 'B6': sheet7_B6,
                'C6': sheet7_C6, 'B7': sheet7_B7, 'C7': sheet7_C7, 'D7': sheet7_D7, 'B8': sheet7_B8,
                'C8': sheet7_C8, 'D8': sheet7_D8, 'B9': sheet7_B9, 'C9': sheet7_C9, 'B13': sheet7_B13,
                'C13': sheet7_C13,
                'D13': sheet7_D13, 'B14': sheet7_B14, 'C14': sheet7_C14,
                'D14': sheet7_D14, 'type2_best_B6': type2_best_B6, 'type2_best_C6': type2_best_C6,
                'type3_best_B4_ADD': type3_best_B4_ADD, 'type3_best_C4_ADD': type3_best_C4_ADD,
                'type4_best_B4_ADD': type4_best_B4_ADD, 'type4_best_C4_ADD': type4_best_C4_ADD,
                'type4_best_total_ADD': type4_best_total_ADD}
        #timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        print(data)



    else:
        pass


if __name__ == '__main__':
    start = datetime.datetime.now()


    model()

    elapsed = float((datetime.datetime.now() - start).seconds)
    print("Time Used 4 All ----->>>> %f seconds" % (elapsed))

    sys.exit()
